﻿using Microsoft.Practices.Prism.Mvvm;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Songhay.Extensions;

namespace Songhay.BiggestBox.Desktop.Modules.ODataWeather.ViewModels
{
    using ModelContext;

    public class DataPointsViewModel : BindableBase
    {
        public DataPointsViewModel()
        {
            this.DataPoints = new ObservableCollection<DataPointViewModel>();

            this.LoadDataPointsAsync();
        }

        public ObservableCollection<DataPointViewModel> DataPoints
        {
            get { return this._dataPoints; }
            set { this.SetProperty(ref this._dataPoints, value); }
        }

        public bool IsDoingServiceOperation
        {
            get { return this._isDoingServiceOperation; }
            set { this.SetProperty(ref this._isDoingServiceOperation, value); }
        }

        async void LoadDataPointsAsync()
        {
            this.IsDoingServiceOperation = true;

            var data = await CambridgeWeatherContext.LoadDataPointsAsync();

            this.DataPoints.Clear();
            data.ForEachInEnumerable(i => this.DataPoints.Add(i));

            this.IsDoingServiceOperation = false;
        }

        bool _isDoingServiceOperation;
        ObservableCollection<DataPointViewModel> _dataPoints;
    }
}
